import { Controller, Get, Post, Body, Put, Param, Query, Delete, Res } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiConsumes, ApiQuery, ApiBearerAuth } from '@nestjs/swagger';
import { RoleService } from './role.service';
import { Response } from 'express';
import { CreateRoleDto, UpdateRoleDto, ListRoleDto, ChangeRoleStatusDto, AuthUserCancelDto, AuthUserCancelAllDto, AuthUserSelectAllDto } from './dto/index';
import { AllocatedListDto } from '../user/dto/index';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';

import { UserService } from '../user/user.service';
import { User, UserDto } from 'src/module/system/user/user.decorator';

@ApiTags('角色管理')
@Controller('system/role')
export class RoleController {
    constructor(
        private readonly roleService: RoleService,
        private readonly userService: UserService,
    ) { }

    @ApiOperation({
        summary: '角色管理-创建',
    })
    @ApiBody({
        type: CreateRoleDto,
        required: true,
    })
    @RequirePermission('system:role:add')
    @Post()
    create(@Body() createRoleDto: CreateRoleDto) {
        return this.roleService.create(createRoleDto);
    }

    @ApiOperation({
        summary: '角色管理-列表',
    })
    @ApiBody({
        type: ListRoleDto,
        required: true,
    })
    @RequirePermission('system:role:list')
    @Get('list')
    findAll(@Query() query: ListRoleDto, @User() user: UserDto) {
        return this.roleService.findAll(query);
    }

    @ApiOperation({
        summary: '角色管理-部门树',
    })
    @RequirePermission('system:role:edit')
    @Get('deptTree/:id')
    deptTree(@Param('id') id: string) {
        return this.roleService.deptTree(+id);
    }

    @ApiOperation({
        summary: '角色管理-详情',
    })
    @RequirePermission('system:role:query')
    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.roleService.findOne(+id);
    }

    @ApiOperation({
        summary: '角色管理-修改',
    })
    @ApiBody({
        type: UpdateRoleDto,
        required: true,
    })
    @RequirePermission('system:role:edit')
    @Put()
    async update(@Body() updateRoleDto: UpdateRoleDto, @User() user: UserDto) {
        const result = await this.roleService.update(updateRoleDto)
        await this.userService.updateRedisUserRolesAndPermissions(user.token, user.user.userId);
        return result;
    }

    @ApiOperation({
        summary: '角色管理-数据权限修改',
    })
    @ApiBody({
        type: UpdateRoleDto,
        required: true,
    })
    @RequirePermission('system:role:edit')
    @Put('dataScope')
    dataScope(@Body() updateRoleDto: UpdateRoleDto) {
        return this.roleService.dataScope(updateRoleDto);
    }

    @ApiOperation({
        summary: '角色管理-停用角色',
    })
    @ApiBody({
        type: ChangeRoleStatusDto,
        required: true,
    })
    @RequirePermission('system:role:edit')
    @Put('changeStatus')
    changeStatus(@Body() changeRoleStatusDto: ChangeRoleStatusDto) {
        return this.roleService.changeStatus(changeRoleStatusDto);
    }

    @RequirePermission('system:role:remove')
    @Delete(':id')
    async remove(@Param('id') ids: string, @User() user: UserDto) {
        const userId = user.user.userId;
        const uuid = user.token;
        const menuIds = ids.split(',').map((id) => +id);

        const result = await this.roleService.remove(menuIds);
        await this.userService.updateRedisUserRolesAndPermissions(uuid, userId);

        return result;
    }

    @ApiOperation({
        summary: '角色管理-角色已分配用户列表',
    })
    @ApiBody({
        type: AllocatedListDto,
        required: true,
    })
    @RequirePermission('system:role:query')
    @Get('authUser/allocatedList')
    authUserAllocatedList(@Query() query: AllocatedListDto) {
        return this.userService.allocatedList(query);
    }

    @ApiOperation({
        summary: '角色管理-角色未分配用户列表',
    })
    @ApiBody({
        type: AllocatedListDto,
        required: true,
    })
    @RequirePermission('system:role:query')
    @Get('authUser/unallocatedList')
    authUserUnAllocatedList(@Query() query: AllocatedListDto) {
        return this.userService.unallocatedList(query);
    }

    @ApiOperation({
        summary: '角色管理-解绑角色',
    })
    @ApiBody({
        type: AuthUserCancelDto,
        required: true,
    })
    @RequirePermission('system:role:edit')
    @Put('authUser/cancel')
    authUserCancel(@Body() body: AuthUserCancelDto) {
        return this.userService.authUserCancel(body);
    }

    @ApiOperation({
        summary: '角色管理-批量解绑角色',
    })
    @ApiBody({
        type: AuthUserCancelAllDto,
        required: true,
    })
    @RequirePermission('system:role:edit')
    @Put('authUser/cancelAll')
    authUserCancelAll(@Body() body: AuthUserCancelAllDto) {
        return this.userService.authUserCancelAll(body);
    }

    @ApiOperation({
        summary: '角色管理-批量绑定角色',
    })
    @ApiBody({
        type: AuthUserSelectAllDto,
        required: true,
    })
    @RequirePermission('system:role:edit')
    @Put('authUser/selectAll')
    authUserSelectAll(@Body() body: AuthUserSelectAllDto) {
        return this.userService.authUserSelectAll(body);
    }

    @ApiOperation({ summary: '导出角色管理xlsx文件' })
    @RequirePermission('system:role:export')
    @Post('/export')
    async export(@Res() res: Response, @Body() body: ListRoleDto): Promise<void> {
        return this.roleService.export(res, body);
    }
}
